from rest_framework.viewsets import ModelViewSet
from goods.models import SKU,SPU
from meiduo_admin.my_paginate import MyPageNuberPagination
from .skus_serializers import SKUVieSetSerializers,SKUCategorySerializer,SKUGoodSimpleSerializer,SPUSpecificationerializers
from rest_framework.generics import ListAPIView
from goods.models import GoodsCategory,SPUSpecification



class SKUViewSet(ModelViewSet):
    pagination_class = MyPageNuberPagination
    serializer_class = SKUVieSetSerializers
    queryset = SKU.objects.all()

    #重写get_queryset方法,使之已keyword来给前段数据
    def get_queryset(self):
        #1.获取keyword
        keyword=self.request.query_params.get("keyword")

        #2.判断是否有keyword,有就返回他需要的包含该keyword的商品返回去
        if keyword:
            return SKU.objects.filter(name__contains=keyword).all()

        else:
            return SKU.objects.all()
#新增商品sku分类
class SKUCategoryView(ListAPIView):
    serializer_class = SKUCategorySerializer
    # related_name='subs',相当于该模型的子级,当子级为None就表明他已经没有子级了,也就是他就是最后的子级,美多最小就是第三级,所以subs=None就是3级分类
    queryset = GoodsCategory.objects.filter(subs=None).all()
# 新增商品spu分类
class SKUGoodSimpleView(ListAPIView):
    serializer_class=SKUGoodSimpleSerializer
    queryset=SPU.objects.all()
#新增商品spu规格
class SKUGoodSpecsView(ListAPIView):
    serializer_class =SPUSpecificationerializers
    # queryset = SpecificationOption.objects.all()
    def get_queryset(self):
        #1.获取参数
        spu_id=self.kwargs.get("spu_id")
        #2.查询规格
        return SPUSpecification.objects.filter(spu_id=spu_id).all()

